package com.fas.system.controller;

import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.model.LoginUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.utils.SecurityUtils;
import com.fas.common.utils.poi.ExcelUtil;
import com.fas.system.domain.FasFacilities;
import com.fas.system.domain.FasFacilitiesType;
import com.fas.system.domain.vo.FacilityDataItemVo;
import com.fas.system.service.IFasFacilitiesService;
import com.fas.system.service.IFasFacilitiesTypeService;
import com.fas.system.service.ISysDeptService;
import com.fas.system.util.OrderldUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 设施信息Controller
 *
 * @author ruoyi
 * @date 2022-05-05
 */
@Api(tags = "设施信息")
@RestController
@RequestMapping("/system/facilities")
public class FasFacilitiesController extends BaseController
{
    @Autowired
    private IFasFacilitiesService fasFacilitiesService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private OrderldUtil orderldUtil;
    @Resource
    private IFasFacilitiesTypeService fasFacilitiesTypeService;

    @ApiOperation(value = "维保单位查询")
//    @PreAuthorize("@ss.hasPermi('system:equipment:mainte')")
    @GetMapping("/mainte")
    public TableDataInfo findAllEquipment(@RequestParam(value = "condition",required = false)String condition){
        System.out.println("输入的参数："+ condition);
        startPage();
        LoginUser user = SecurityUtils.getLoginUser();
        FasFacilities fasFacilities=new FasFacilities();
        fasFacilities.setDeptId(user.getDeptId());
        fasFacilities.setCondition(condition);
        List<FasFacilities> likeFacility = fasFacilitiesService.findLikeFacility(fasFacilities);
        System.out.println(likeFacility);
        return getDataTable(likeFacility);
    }

//    @ApiOperation(value = "总公司查询")
    @ApiOperation(value = "设施列表查询")
//    @PreAuthorize("@ss.hasPermi('system:equipment:findLikeFacility')")
    @GetMapping("/findLikeFacility")
    public TableDataInfo findLikeFacility(FasFacilities fasFacilities,Integer pageNum, Integer pageSize){
        System.out.println("输入的参数："+ fasFacilities.toString());
        LoginUser user = SecurityUtils.getLoginUser();
        Long deptId = user.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);

        //总公司
        if("0".equals(sysDept.getDeptType())){
            startPage();
            List<FasFacilities> identityfind = fasFacilitiesService.findAllFacility(fasFacilities);
            return getDataTable(identityfind);
        }else if("2".equals(sysDept.getDeptType())){//维保单位
            fasFacilities.setDeptId(deptId);
            startPage();
            List<FasFacilities> likeFacility = fasFacilitiesService.findLikeFacility(fasFacilities);
            return getDataTable(likeFacility);
        }
        return null;
    }

    @ApiOperation(value = "子公司查询")
//    @PreAuthorize("@ss.hasPermi('system:equipment:findLikeCompanyOrArea')")
    @GetMapping("/findLikeCompanyOrArea")
    public TableDataInfo findLikeCompanyOrArea(@RequestParam(value = "condition",required = false)String condition
            ,@RequestParam(value = "deptId",required = false)Long deptId){
        startPage();
        System.out.println("输入的参数："+ condition);
        FasFacilities fasFacilities=new FasFacilities();
        fasFacilities.setDeptId(deptId);
        fasFacilities.setCondition(condition);
        List<FasFacilities> likeFacility = fasFacilitiesService.findLikeFacility(fasFacilities);
        System.out.println(likeFacility);
        return getDataTable(likeFacility);
    }

    @ApiOperation(value = "微信设施查询")
//    @PreAuthorize("@ss.hasPermi('system:equipment:wxfindallequment')")
    @GetMapping("/wxfindallequment")
    public TableDataInfo wxfindallequment(@RequestParam(value = "condition",required = false)String condition,
                                          Integer pageNum,Integer pageSize,String buildingId){
        startPage();
        LoginUser user = SecurityUtils.getLoginUser();
        FasFacilities fasFacilities=new FasFacilities();
        fasFacilities.setDeptId(user.getDeptId());
        fasFacilities.setCondition(condition);
        fasFacilities.setBuildingid(buildingId);
        List<FasFacilities> likeFacility = fasFacilitiesService.findLikeFacility(fasFacilities);
        return getDataTable(likeFacility);
    }

    @ApiOperation(value = "设施查询详细信息")
    @GetMapping("/facilityDetails")
    public TableDataInfo facilityDetails(Integer id){
//        startPage();
        List<FasFacilities> fasFacilities = fasFacilitiesService.facilityDetails(id);
        return getDataTable(fasFacilities);
    }

    /**
     * 查询设施信息列表
     */
    @ApiOperation(value = "查询设施信息列表")
//    @PreAuthorize("@ss.hasPermi('system:facilities:list')")
    @GetMapping("/list")
    public TableDataInfo list(FasFacilities fasFacilities)
    {
        startPage();
        List<FasFacilities> list = fasFacilitiesService.selectFasFacilitiesList(fasFacilities);
        return getDataTable(list);
    }

    /**
     * 导出设施信息列表
     */
    @ApiOperation(value = "导出设施信息列表")
//    @PreAuthorize("@ss.hasPermi('system:facilities:export')")
    @Log(title = "设施信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody FasFacilities fasFacilities) {
        System.out.println("输入的参数："+ fasFacilities.toString());
        LoginUser user = SecurityUtils.getLoginUser();
        Long deptId = user.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        List<FasFacilities> list = new ArrayList<>();
        if("0".equals(sysDept.getDeptType())){
            list = fasFacilitiesService.findAllFacility(fasFacilities);
        }else if("2".equals(sysDept.getDeptType())){
            fasFacilities.setDeptId(deptId);
            list = fasFacilitiesService.findLikeFacility(fasFacilities);
        }

        // 查询设施类型名称
        List<FasFacilitiesType> typeList = fasFacilitiesTypeService.findList(new FasFacilitiesType());
        if (list.size() > 0){
            for (FasFacilities facilities : list) {
                for (FasFacilitiesType fasFacilitiesType : typeList) {
                    if (facilities.getFacilityType() != null && Objects.equals(facilities.getFacilityType(), fasFacilitiesType.getId())){
                        facilities.setFacilityTypeName(fasFacilitiesType.getFacilityName());
                        break;
                    }
                }
            }
        }
        ExcelUtil<FasFacilities> util = new ExcelUtil<FasFacilities>(FasFacilities.class);
        util.exportExcel(response, list, "设施信息数据");
    }


    /**
     * 批量导入设施信息
     * @param file
     * @return
     */
    @PostMapping("/importData")
    @Log(title = "设施信息", businessType = BusinessType.IMPORT)
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<FasFacilities> util = new ExcelUtil<>(FasFacilities.class);
            List<FasFacilities> facilitiesList = util.importExcel(file.getInputStream());
            facilitiesList.forEach(e -> e.setDeptId(getDeptId()));
            return fasFacilitiesService.importFasFacilities(facilitiesList);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取设施信息详细信息
     */
    @ApiOperation(value = "获取设施信息详细信息")
//    @PreAuthorize("@ss.hasPermi('system:facilities:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fasFacilitiesService.selectFasFacilitiesById(id));
    }

    /**
     * 新增设施信息
     */
    @ApiOperation(value = "新增设施信息")
//    @PreAuthorize("@ss.hasPermi('system:facilities:add')")
    @Log(title = "设施信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FasFacilities fasFacilities)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasFacilities.setDeptId(loginUser.getDeptId());
//        String facilityNo = orderldUtil.getFacilityNo();
//        fasFacilities.setFacilityNumber(facilityNo);
        return fasFacilitiesService.insertFasFacilities(fasFacilities);
    }

    /**
     * 修改设施信息
     */
    @ApiOperation(value = "修改设施信息")
//    @PreAuthorize("@ss.hasPermi('system:facilities:edit')")
    @Log(title = "设施信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FasFacilities fasFacilities)
    {
        return toAjax(fasFacilitiesService.updateFasFacilities(fasFacilities));
    }

    /**
     * 删除设施信息
     */
    @ApiOperation(value = "删除设施信息")
//    @PreAuthorize("@ss.hasPermi('system:facilities:remove')")
    @Log(title = "设施信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fasFacilitiesService.deleteFasFacilitiesByIds(ids));
    }


    /**
     * （已弃用）
     * 小程序查询当前巡检员下的当前线路下的建筑
     */
    @ApiOperation(value = "小程序查询当前巡检员下的当前线路下的建筑")
    @Log(title = "设施信息", businessType = BusinessType.DELETE)
    @GetMapping("/miniappFindIpqcBuilding/{lineId}")
    public TableDataInfo miniappFindIpqcBuilding(@PathVariable Long lineId){
        return getDataTable(fasFacilitiesService.miniappFindIpqcBuilding(lineId));
    }

    @ApiOperation(value = "小程序巡检员-分配线路列表-线路列表-线路详情建筑列表-设施详情")
    @GetMapping("/miniappIpqcLineBuildingFacility")
    public TableDataInfo miniappIpqcLineBuildingFacility(Integer facilityId,Long taskId){
        List<FacilityDataItemVo> list = fasFacilitiesService.miniappIpqcLineBuildingFacility(facilityId,taskId);
        return getDataTable(list);
    }

}
